package com.race.car;

import java.util.Random;

import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;

import com.race.map.RaceMap;

public class BAK_NPCCar extends BAK_YACar{

	
	protected float lastX,lastY;
	protected int lastDirection;
	public BAK_NPCCar(float pX, float pY, ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,RaceMap raceMap) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager,raceMap);
		
	}
	public void randomMove(){
		new Thread(new Runnable(){
			Random rnd=new Random(System.nanoTime());
			@Override
			public void run() {
				lastX=mCarBody.getWorldCenter().x;
				lastY=mCarBody.getWorldCenter().y;
				while(true){
//					Log.i("NPC"+this.toString(),"lastX:"+lastX+" lastY"+lastY);
					onVelocityChange(5f);
					float a=getAngle();
					float curX=mCarBody.getWorldCenter().x,curY=mCarBody.getWorldCenter().y;
					Vector2 v=getVelocity();
//					Vector2 v=Vector2Pool.obtain(curX-lastX,curY-lastY);
					Vector2 d=Vector2Pool.obtain((float)Math.cos(a),(float)Math.sin(a));
					float dot=v.dot(d);
					if(dot>-0.1){
						if(dot>0.1)setAngle(MathUtils.atan2(v.y, v.x));
						else{
							onRotationChange((float)Math.PI/2+(float)(Math.random()-0.5)/100);
						}
					}
					Vector2Pool.recycle(d);
//					Vector2Pool.recycle(v);
					lastX=curX;
					lastY=curY;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
		}).start();
	}
}
